/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facades;

import entities.User;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author damfercer
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {
    @PersistenceContext(unitName = "ProyectoPU")
    private EntityManager em;
    @Inject
    private CandidateFacade candidateFacade;

    @Override
    public EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }
    
    public boolean isEmailRegistered(String email){
        List<User> users=em.createNamedQuery("User.findByEmail").setParameter("email", email).getResultList();
        return !users.isEmpty();
    }
    
    public User getUserFromEmailAndPassword(String email, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        User res=null;
        Query namedQuery=em.createNamedQuery("User.findByEmailAndPassword",User.class);
        namedQuery.setParameter("email", email);
        namedQuery.setParameter("password", password);
        if(!namedQuery.getResultList().isEmpty()){
           res=(User)namedQuery.getSingleResult(); 
        }
        return res;
    }
    
    public User getUserFromEmail(String email){
        User res=null;
        Query namedQuery=em.createNamedQuery("User.findByEmail");
        namedQuery.setParameter("email", email);
        if(!namedQuery.getResultList().isEmpty()){
           res=(User)namedQuery.getSingleResult(); 
        }
        return res;
    }
}
